/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.server.globalManager.logic;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import mscs.data.IFileDataStore;
import mscs.data.IGoodsProxy;
import mscs.data.pojo.Goods;
import mscs.data.pojo.Person;

/**
 *
 * @author Administrator
 */
public class EditGoodsInfoUtil implements IEditGoodsInfoUtil{
    private IFileDataStore fds = new IGoodsProxy();
    private ArrayList<Goods> newList = new ArrayList<Goods>();
    private ArrayList<Integer> indexes = new ArrayList<Integer>();
    private ArrayList<Goods> allList;
    
    public EditGoodsInfoUtil(){
        allList = (ArrayList<Goods>) fds.getObjects();
    }

    public int getIndexInall(Goods goods) {
        for(int i=0; i<allList.size(); i++){
            if(allList.get(i).getId().equals(goods.getId())){
                return i;
            }
        }
        return -1;
    }
    
    public boolean update(int index, Goods goods) {
        if(index<=this.allList.size() && index>=0){
             this.indexes.add(index);
             this.newList.add(goods);
             return true;
        }
        return false;
    }

    public boolean removeFromUpdatedList(int index) {
        if(index<=indexes.size() && index>=0){
            this.indexes.remove(index);
            this.newList.remove(index);
            return true;
        }
        return false;
    }

    public boolean commit() {
        for(int i=0; i<indexes.size(); i++){
            allList.set(this.indexes.get(i), this.newList.get(i));
        }
        fds.update(allList);
        this.newList.clear();
        this.indexes.clear();
        return true;
    }

    public List<Goods> getUpdatedList() {
        return this.newList;
    }

 


    
    
}
